
 
/* --------------------------------------- Include Files ---------------------------------------- */
//#include "common.h"
#include "stm32f10x.h"
#include "system_timer.h"
#include "driver.h"
#include "uthread.h"
#include "sip.h"
#include "parameter.h"
#include "config.h"


/* ---------------------------------------- Macro Defines --------------------------------------- */


/* -------------------------------------- Data Type Defines ------------------------------------- */


/* --------------------------------- Global Function Prototypes --------------------------------- */
void Cfg_handleRxThread(void);
void Cfg_RxHandler(SIP_PARSE_STATE* frame_state);

/* --------------------------------- Private Function Prototypes -------------------------------- */
static void Cfg_handleReadRequest(SIP_PARSE_STATE* frame_state);
static void Cfg_handleWriteRequest(SIP_PARSE_STATE* frame_state);
//static void Cfg_handleCtrlRequest(SIP_PARSE_STATE* frame_state);
//static void Cfg_handleFileRequest(SIP_PARSE_STATE* frame_state);


/* -------------------------------------- Global Variables -------------------------------------- */


/* -------------------------------------- Static Variables -------------------------------------- */
static SIP_FRAME sip_frame;


 /**
  * @brief Cfg_handleRxThread
  * @param  
  * @retval 
  * @note   
  */
void Cfg_handleRxThread(void)
{
	//static uint32_t result = 0x00;
	static uint8_t rxdata;
	static SIP_PARSE_STATE parse_state = {.state=0X01, .pt=0};

	_scode
	while(1)
	{
		while(Uart1_getRxRingbufferN2RLength()!=0){
			Uart1_getRxRingbufferData(&rxdata,1);
			Sip_parseFrame(&parse_state, Cfg_RxHandler, rxdata);
		}		
		_sleep(20);
	}
	_ecode
}


/**
  * @brief Cfg_RxHandler
  * @param  
  * @retval 
  * @note   
  */
void Cfg_RxHandler(SIP_PARSE_STATE* frame_state)
{

	uint8_t type = 0;
	
	type = frame_state->type;
	if((type&0x10)==0){                                  // 读,写
		if(type == 0x01){ 
			Cfg_handleReadRequest(frame_state);
		}else if(type == 0x02){ 
		    Cfg_handleWriteRequest(frame_state);
		}
	}else{                                               // 读回复,写回复
		if(type == 0x11){ 

		}else if(type == 0x12){

		}		
	}
}

/*************************************************************************************************
 * Procedure: Cfg_handleReadRequest
 * Object: 读取配置参数
 *    
 * Parameters In:
 * - none
 * Parameters Out:
 * - none
 *************************************************************************************************/
void Cfg_handleReadRequest(SIP_PARSE_STATE* frame_state)
{
	uint16_t pid = 0;
	//uint32_t len = 0;
	uint8_t buffer[128] = {0x00};
	uint32_t size = 0;
	uint32_t result = 0x00;
	
	pid = (uint16_t)frame_state->rxframe.content_and_checksum[0] + ((uint16_t)frame_state->rxframe.content_and_checksum[1])*256; 
	result = Dbs_getParValue(pid, &buffer[5], &size);
	buffer[0] = (uint8_t)result;
	buffer[1] = (uint8_t)pid&0xFF;
	buffer[2] = (uint8_t)(pid>>8)&0xFF;
	buffer[3] = (uint8_t)size&0xFF;
	buffer[4] = (uint8_t)(size>>8)&0xFF;
	sip_frame = Sip_createFrame(0x11, buffer, 5+size);	
	Uart1_writeBytes((uint8_t*)&sip_frame, sip_frame.length[0]+(((uint16_t)sip_frame.length[1])<<8));
}


/**
  * @brief Cfg_handleWriteRequest
  * @param  
  * @retval 
  * @note   写入配置参数
  */
void Cfg_handleWriteRequest(SIP_PARSE_STATE* frame_state)
{
	uint8_t buffer[128] = {0x00};
	uint16_t pid = 0;
	uint32_t len = 0;
	uint32_t result = 0x00;
	
	// pid = 2 is Sn number
	pid = (uint16_t)frame_state->rxframe.content_and_checksum[0] + ((uint16_t)frame_state->rxframe.content_and_checksum[1])*256;
	len = (uint16_t)frame_state->rxframe.content_and_checksum[2] + ((uint16_t)frame_state->rxframe.content_and_checksum[3])*256;
	result = Dbs_setParValue(dbs_par_pk_locksn, &frame_state->rxframe.content_and_checksum[4], len);
	buffer[0] = (uint8_t)result;
	buffer[1] = (uint8_t)pid&0xFF;
	buffer[2] = (uint8_t)(pid>>8)&0xFF;
    sip_frame = Sip_createFrame( 0x12, buffer, 3);	
	#if HEX2STR
	#else
		Uart1_writeBytes((uint8_t*)&sip_frame, sip_frame.length[0]+(((uint16_t)sip_frame.length[1])<<8));
	#endif
}










